package com.zzyl.nursing.controller;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.dto.NursingTaskDto;
import com.zzyl.nursing.dto.NursingTaskUpdateDto;
import com.zzyl.nursing.service.INursingTaskService;
import com.zzyl.nursing.vo.NursingTaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

/**
 * 护理任务Controller
 * 
 * @author ruoyi
 * @date 2025-10-29
 */
@RestController
@RequestMapping("/nursing/nursingTask")
@Api(tags = "护理任务相关接口")
public class NursingTaskController extends BaseController
{
    @Autowired
    private INursingTaskService nursingTaskService;

    /**
     * 查询护理任务列表
     */
//    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:list')")
//    @GetMapping("/list")
//    @ApiOperation(value = "获取护理任务列表")
//    public TableDataInfo<List<NursingTask>> list(NursingTask nursingTask)
//    {
//        startPage();
//        List<NursingTask> list = nursingTaskService.selectNursingTaskList(nursingTask);
//        return getDataTable(list);
//    }
//    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:list')")
//    @GetMapping("/list")
//    @ApiOperation(value = "获取护理任务列表")
//    public TableDataInfo<List<NursingTask>> list(NursingTaskDto dto)
//    {
//        startPage();
//        List<NursingTaskVo> list = nursingTaskService.selectNursingTaskList(dto);
//        return getDataTable(list);
//    }
    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long nurseId, // 前端传的是 nurseId
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String elderName,
            @RequestParam(required = false) Long projectId)

    {

        startPage();

        NursingTaskDto dto = new NursingTaskDto();
        // 字段名映射：前端 nurseId → 后端 nursingId（但注意类型！）
        dto.setNursingId(nurseId); // 先保留 Long，后面再说

        dto.setStatus(status);
        dto.setElderName(elderName); // 如果有其他条件可设
        dto.setProjectId(projectId);

        // 手动解析时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

// 处理 startTime
        if (StringUtils.hasText(startTime)) {
            LocalDateTime start = LocalDateTime.parse(startTime, formatter);
            if (dto.getParams() == null) {
                dto.setParams(new HashMap<>()); // 初始化 params
            }
            dto.getParams().put("startTime", start);
        }

// 处理 endTime
        if (StringUtils.hasText(endTime)) {
            LocalDateTime end = LocalDateTime.parse(endTime, formatter);
            if (dto.getParams() == null) {
                dto.setParams(new HashMap<>());
            }
            dto.getParams().put("endTime", end);
        }

        List<NursingTaskVo> list = nursingTaskService.selectNursingTaskList(dto);
        return getDataTable(list);
    }

    /**
     * 导出护理任务列表
     */
//    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:export')")
//    @Log(title = "护理任务", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ApiOperation("导出护理任务列表")
//    public void export(HttpServletResponse response, NursingTask nursingTask)
//    {
//        List<NursingTask> list = nursingTaskService.selectNursingTaskList(nursingTask);
//        ExcelUtil<NursingTask> util = new ExcelUtil<NursingTask>(NursingTask.class);
//        util.exportExcel(response, list, "护理任务数据");
//    }

    /**
     * 获取护理任务详细信息
     */
//    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:query')")
//    @GetMapping(value = "/{id}")
//    @ApiOperation("获取护理任务详细信息")
//    public R<NursingTask> getInfo(@ApiParam(value = "护理任务ID", required = true)
//            @PathVariable("id") Long id)
//    {
//        return R.ok(nursingTaskService.selectNursingTaskById(id));
//    }
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取护理任务详细信息")
    public R<NursingTaskVo> getInfo(@ApiParam(value = "护理任务ID", required = true)
                                            @PathVariable("id") Long id)
    {
        return R.ok(nursingTaskService.getInfo(id));
    }

    /**
     * 新增护理任务
     */
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:add')")
    @Log(title = "护理任务", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增护理任务")
    public AjaxResult add(@ApiParam(value = "护理任务实体", required = true) @RequestBody NursingTask nursingTask)
    {
        return toAjax(nursingTaskService.insertNursingTask(nursingTask));
    }

    /**
     * 修改护理任务
     */
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:edit')")
    @Log(title = "护理任务", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改护理任务")
    public AjaxResult edit(@ApiParam(value = "护理任务实体", required = true) @RequestBody NursingTask nursingTask)
    {
        return toAjax(nursingTaskService.updateNursingTask(nursingTask));
    }

    /**
     * 删除护理任务
     */
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:remove')")
    @Log(title = "护理任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除护理任务")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids)
    {
        return toAjax(nursingTaskService.deleteNursingTaskByIds(ids));
    }

    /**
     * 取消任务
     * @param dto
     * @return
     */
    @PutMapping("/cancel")
    public AjaxResult cancel(@RequestBody NursingTaskUpdateDto dto){
        nursingTaskService.cancel(dto);
        return success();
    }

    @PutMapping("/do")
    public AjaxResult doit(@RequestBody NursingTaskUpdateDto dto){
        nursingTaskService.doit(dto);
        return success();
    }
    @PutMapping("/updateTime")
    public AjaxResult updateTime(@RequestBody NursingTaskUpdateDto dto){
        nursingTaskService.updateTime(dto);
        return success();
    }

}
